#include "utils.h"
#include <iostream>
#include <string.h>

extern "C" {

    #include "wireguard.h"
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
}

utils::utils()
{

    // acquire device names
    char *device_names, *device_name;
    size_t len;
    int num=0;
    device_names = wg_list_device_names();
    if (!device_names) {
        perror("Unable to get device names");
        exit(1);
    }
    wg_for_each_device_name(device_names, device_name, len){
//        std::cout << device_name << std::endl;
        deviceName[num]= device_name;
        num+=1;
    }
    numOfIF=num;
//    free(device_names);

}

wg_device * utils::getDeviceBYName(const char * deviceName){
    wg_device *device;

    if (wg_get_device(&device, deviceName) < 0){
        perror("Unable to get device");
        return 0;
    }

//    std::cout << device->name << std::endl;
    return device;
}


std::string utils::wgKey2Base64(const wg_key key){
        if(nullptr==keystring)
        keystring =  (char *)malloc(32);
        wg_key_to_base64(keystring,  key);
        std::string key_str(keystring);
        return key_str;
}



void utils::addInterFace( const std::string name){

     const char* c_str = name.c_str();
     wg_add_device(c_str);

}


utils::~utils(){
    free(keystring);
}



